package com.forhappy.light3ds8.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.forhappy.light3ds8.R;

public class MainUI extends Activity{
	private static final String TAG = "MainUI";
	ViewPager viewPage = null;
	static Activity g_Activity = null;
	public static Activity getActivity(){
		return g_Activity;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		g_Activity = this;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_view_pager);
		initViewPage();
	}

	public void initViewPage(){
	        PagerAdapter pagerAdapter = new MainPageAdapter(this);
			viewPage = (ViewPager) findViewById(R.id.viewpager);
			viewPage.setAdapter(pagerAdapter);
			viewPage.setOnPageChangeListener(new OnPageChangeListener() {
				@Override
				public void onPageSelected(int pos) {
					int[] indicateArray = {R.id.indicate01, R.id.indicate02, R.id.indicate03};
					int needShowId = indicateArray[pos];
					for(int id:indicateArray){
						View view = findViewById(id);
						if(null == view){
							Log.e(TAG, "no view for:" + id);
							return;
						}
						if(needShowId == id){
							view.setVisibility(View.VISIBLE);
						} else {
							view.setVisibility(View.INVISIBLE);
						}
					}
				}
				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {
				}

				@Override
				public void onPageScrollStateChanged(int state) {
				}
			});

			final int[] header = {R.id.info01, R.id.info02, R.id.info03};
			for(int i = 0; i < header.length; i++){
				View tv = findViewById(header[i]);
				tv.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						for(int j =0; j < header.length; j++){
							if(v.getId() == header[j]){
								viewPage.setCurrentItem(j);
								break;
							}
						}
					}
				});
			}
	}

	static class MainPageAdapter extends PagerAdapter{
		ArrayList<View> viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
		static String[] pageTitle = {"发送数据", "下载数据", "离线数据"};
		public MainPageAdapter(Context context){
			LayoutInflater lf = LayoutInflater.from(context);
	        viewList.add(lf.inflate(R.layout.pg1_get_from_server, null));
	        viewList.add(lf.inflate(R.layout.pg2_send_to_arm, null));
	        viewList.add(lf.inflate(R.layout.pg3_download_datas, null));
		}
		private void setOnClickListener(){

		}
		@Override
		public int getCount() {
			return viewList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = viewList.get(position);
			container.addView(view, position);
			return view;
		}
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(viewList.get(position));
		}
		@Override
		public CharSequence getPageTitle(int position) {
			return pageTitle[position];
		}
	}
}
